package com.jeesite.modules.wx.service.serviceImpl;

import com.jeesite.modules.common.refun.*;
import com.jeesite.modules.wx.service.RefunService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class RefunServiceImpl implements RefunService {

    private static final String MCH_ID = "1504722971";
    private static final String API_SECRET = "ZRgZoUq5b0MKkO5gDWyc1MNQCdGHu5cK";
    private static String APP_ID = "wxc86ac9d5537decf6";
    /**
     *  企业付款api
     */
    private static final String TRANSFERS_PAY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";


    @Override
    public synchronized Boolean transferPay(HttpServletRequest request, HttpServletResponse response,String openId,String amount) {
        log.info("转账的openId={}",openId);
        log.info("转账的amount={}",amount);
        String callback = "callback";

        log.info("[/transfer/pay]");
        //业务判断 openid是否有收款资格

        Map<String, String> restmap = null;
        try {
            Map<String, String> parm = new HashMap<String, String>();
            //公众账号appid
            parm.put("mch_appid", APP_ID);
            //商户号
            parm.put("mchid", MCH_ID);
            //随机字符串
            parm.put("nonce_str", PayUtil.getNonceStr());
            //商户订单号
            parm.put("partner_trade_no", PayUtil.getTransferNo());
            //用户openid
            parm.put("openid", openId);
            //校验用户姓名选项 OPTION_CHECK
            parm.put("check_name", "NO_CHECK");
            //parm.put("re_user_name", "安迪"); //check_name设置为FORCE_CHECK或OPTION_CHECK，则必填
            //转账金额
            parm.put("amount", amount);
            //企业付款描述信息
            parm.put("desc", "易客易得");
            //Ip地址
            parm.put("spbill_create_ip", PayUtil.getLocalIp(request));
            parm.put("sign", PayUtil.getSign(parm, API_SECRET));

            String restxml = HttpUtils.posts(TRANSFERS_PAY, XmlUtil.xmlFormat(parm, false));
            restmap = XmlUtil.xmlParse(restxml);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        if (CollectionUtil.isNotEmpty(restmap) && "SUCCESS".equals(restmap.get("result_code"))) {
            log.info("转账成功：" + restmap.get("err_code") + ":" + restmap.get("err_code_des"));
            Map<String, String> transferMap = new HashMap<>();
            //商户转账订单号
            transferMap.put("partner_trade_no", restmap.get("partner_trade_no"));
            //微信订单号
            transferMap.put("payment_no", restmap.get("payment_no"));
            //微信支付成功时间
            transferMap.put("payment_time", restmap.get("payment_time"));
//            WebUtil.response(response,
//                    WebUtil.packJsonp(callback,
//                            JSON.toJSONString(new JsonResult(1, "转账成功", new ResponseData(null, transferMap)),
//                                    SerializerFeatureUtil.FEATURES)));

            return true;

        }else {
            if (CollectionUtil.isNotEmpty(restmap)) {
                log.info("转账失败：" + restmap.get("err_code") + ":" + restmap.get("err_code_des"));
            }
//            WebUtil.response(response, WebUtil.packJsonp(callback, JSON
//                    .toJSONString(new JsonResult(-1, "转账失败", new ResponseData()), SerializerFeatureUtil.FEATURES)));

            return false;
        }
    }
}
